package com.dminvoicemanager;

/*DM_Reports_Generator.java list all the reports and date filters.*/
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.dminvoicemanager.reports.DM_CollectionSumReceipt_Report;
import com.dminvoicemanager.reports.DM_CollectionSumRetailer_Report;
import com.dminvoicemanager.reports.DM_Daily_Unloading_Report;
import com.dminvoicemanager.reports.DM_DamageTransfer_Report;
import com.dminvoicemanager.reports.DM_Loading_Report;
import com.dminvoicemanager.reports.DM_RetailerCard_Report;
import com.dminvoicemanager.reports.DM_Retailer_Discount_Report;
import com.dminvoicemanager.reports.DM_Retailer_Master_Report;
import com.dminvoicemanager.reports.DM_Sales_Summary_Report;
import com.dminvoicemanager.reports.DM_Sales_Summary_Report_Invoice;
import com.dminvoicemanager.reports.DM_Sales_Summary_Report_Retailer;
import com.dminvoicemanager.reports.DM_Sales_Summary_Report_Route;
import com.dminvoicemanager.reports.DM_Sales_Summary_Report_SI;
import com.dminvoicemanager.reports.DM_Sales_Summary_Report_SO;
import com.dminvoicemanager.reports.DM_Sales_Summary_Report_SalOrder;
import com.dminvoicemanager.reports.DM_Stock_InHand_Report;
import com.dminvoicemanager.reports.DM_Target_VS_Actual_Report;
import com.dminvoicemanager.reports.DM_Target_VS_Actual_Report_Sales_Order;
import com.dminvoicemanager.reports.DM_Unloading_Report;
import com.dminvoicemanager.reports.DM_Van_Sales_Summary_Report;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class DM_Reports_Generator extends Activity {
	private String DBNAME = "dminvoicemanager.db";
	private String ITEM_LOCATION = "fLocation";
	private SQLiteDatabase mydb;
	String TagId;
	EditText etcurrentreport;
	Button btnselectreporttype, btndmreportfromdate, btndmreporttodate;
	String fromLocName, fromLocCode = "0", toLocName, toLocCode = "0";
	private int mYear, mMonth, mDay, mHour, mMinute;
	static final int DATE_DIALOG_ID_FROM = 0;
	static final int DATE_DIALOG_ID_TO = 1;
	protected static final String TAG = null;
	AlertDialog InvoiceHeaderAlert = null;
	private AlertDialog myDialog;
	String currentReportID, currentReportName;
	TextView FromLocation, ToLocation;
	private String[] items = { "1. Target Vs Actual",
			"2. Sales Summary(Date Wise Only)",
			"3. Sales Summary(Date & Retailer Wise)",
			"4. Sales Summary(Date & Route Wise)",
			"5. Sales Summary(Date & Invoice Wise)", "6. Loading Report",
			"7. Unloading Report", "8. Damage Transfer Report",
			"9. Retailer Card", "10. Retailer Discount Report",
			"11. Van Sales Summary", "12. Stock In Hand",
			"13. Collection Summary(Date & Receipt Wise)",
			"14. Collection Summary(Date & Retailer Wise)",
			"15. SalesOrder Summary(Date Wise Only)",
			"16. Target Vs Actual(SO)", 
			"17. SI - Sales Summary",
			"18. SO - Sales Summary",
			"19. Daily UnLaoding Report",};
	
	
	int DateTag = 0, CHKLOCTAG = 0;
	private Spinner locationFromSpinner, locationToSpinner;
	private ArrayAdapter<CharSequence> m_adapterForSpinner;
	private ArrayAdapter<CharSequence> m_adapterForSpinnerToLoc;

	// Get Current date and time for Alert Dialog
	public DM_Reports_Generator() {
		// Assign current Date and Time Values to Variables
		final Calendar c = Calendar.getInstance();
		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH);
		mDay = c.get(Calendar.DAY_OF_MONTH);
		mHour = c.get(Calendar.HOUR_OF_DAY);
		mMinute = c.get(Calendar.MINUTE);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dm_reports_generator);
		Bundle bundle = getIntent().getExtras();
		TagId = bundle.getString("TAGID");

		String[] TagIdCurrentReport = TagId.split("/");
		currentReportID = TagIdCurrentReport[0];
		currentReportName = TagIdCurrentReport[1];

		etcurrentreport = (EditText) findViewById(R.id.etcurrentreport);
		btnselectreporttype = (Button) findViewById(R.id.btnselectreporttype);
		btndmreportfromdate = (Button) findViewById(R.id.btndmreportfromdate);
		btndmreporttodate = (Button) findViewById(R.id.btndmreporttodate);
		FromLocation = (TextView) findViewById(R.id.selectedFromLocation);
		ToLocation = (TextView) findViewById(R.id.selectedToLocation);
		locationFromSpinner = (Spinner) findViewById(R.id.spinnerfromlocation);
		locationToSpinner = (Spinner) findViewById(R.id.spinnertolocation);
		locationFromSpinner.setEnabled(false);
		locationToSpinner.setEnabled(false);

		if (currentReportID.equals("6")) {
			locationFromSpinner.setEnabled(true);
			locationToSpinner.setEnabled(true);
			etcurrentreport.setText(currentReportName.replaceAll(
					"[^a-zA-Z.,]+", ""));
			loadFromMainStoreLocationSpinner();
			loadToRepLocationSpinner();
		} else if (currentReportID.equals("7")) {
			locationFromSpinner.setEnabled(true);
			locationToSpinner.setEnabled(true);
			etcurrentreport.setText(currentReportName.replaceAll(
					"[^a-zA-Z.,]+", ""));
			loadFromRepLocationSpinner();
			loadToMainStoreLocationSpinner();
		} else if (currentReportID.equals("8")) {
			locationFromSpinner.setEnabled(true);
			locationToSpinner.setEnabled(true);
			etcurrentreport.setText(currentReportName.replaceAll(
					"[^a-zA-Z.,]+", ""));
			loadFromLocationSpinner();
			loadToDamageLocationSpinner();
		}
		/*
		 * loadFromLocationSpinner(); loadToDamageLocationSpinner();
		 */
		// **********************From Location Spinner**********************

		locationFromSpinner.setAdapter(m_adapterForSpinner);
		locationFromSpinner
				.setOnItemSelectedListener(new OnItemSelectedListener() {
					public void onItemSelected(AdapterView<?> parent,
							View view, int position, long id) {

						fromLocName = locationFromSpinner.getSelectedItem()
								.toString();
						if (!(fromLocName.equals("SELECT FROM LOCATION"))) {

							SQLiteDatabase mydb;
							mydb = openOrCreateDatabase(DBNAME,
									Context.MODE_PRIVATE, null);

							Cursor getfromloccur = null;
							try {
								getfromloccur = mydb.query(ITEM_LOCATION, null,
										"LocName='" + fromLocName + "'", null,
										null, null, null, null);
							} catch (Exception e) {

							}
							if (getfromloccur != null) {
								if (getfromloccur.getCount() > 0) {

									while (getfromloccur.moveToNext()) {
										fromLocCode = getfromloccur
												.getString(9);
										fromLocName = getfromloccur
												.getString(10);
										FromLocation.setText(fromLocCode + ":"
												+ fromLocName);

									}
								}
								getfromloccur.close();
							}

						}

					}

					public void onNothingSelected(AdapterView<?> parent) {
					}
				});

		// **********************To Location Spinner**********************

		locationToSpinner.setAdapter(m_adapterForSpinnerToLoc);
		locationToSpinner
				.setOnItemSelectedListener(new OnItemSelectedListener() {
					public void onItemSelected(AdapterView<?> parent,
							View view, int position, long id) {

						toLocName = locationToSpinner.getSelectedItem()
								.toString();
						if (!(toLocName.equals("SELECT TO LOCATION"))) {

							SQLiteDatabase mydb;
							mydb = openOrCreateDatabase(DBNAME,
									Context.MODE_PRIVATE, null);

							Cursor gettoloccur = null;
							try {
								gettoloccur = mydb.query(ITEM_LOCATION, null,
										"LocName='" + toLocName + "'", null,
										null, null, null, null);
							} catch (Exception e) {

							}
							if (gettoloccur != null) {
								if (gettoloccur.getCount() > 0) {

									while (gettoloccur.moveToNext()) {
										toLocCode = gettoloccur.getString(9);
										toLocName = gettoloccur.getString(10);
										ToLocation.setText(toLocCode + ":"
												+ toLocName);

									}
								}
								gettoloccur.close();
							}

						}

					}

					public void onNothingSelected(AdapterView<?> parent) {
					}
				});

		// ********Display Payment Type List*********
		btnselectreporttype.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				resetFilters();
				myDialog.show();
			}
		});
		// ********Display Payment Type List Alert Dialog*********
		AlertDialog.Builder builder = new AlertDialog.Builder(
				DM_Reports_Generator.this);
		builder.setNegativeButton("EXIT", null);
		builder.setTitle("SELECT REPORT TYPE");
		builder.setIcon(R.drawable.salessummery);
		builder.setItems(items, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

				currentReportName = items[which];
				// String[] accountdetail = selectedReport.split(" ");

				Pattern intsOnly = Pattern.compile("\\d+");
				Matcher makeMatch = intsOnly.matcher(currentReportName);
				makeMatch.find();
				currentReportID = makeMatch.group();

				// currentReportID = accountdetail[0];
				// currentReportName = selectedReport;
				if (currentReportID.equals("6")) {
					reloadView();
				} else if (currentReportID.equals("7")) {
					reloadView();
				} else if (currentReportID.equals("8")) {
					reloadView();
				} else {
					locationFromSpinner.setEnabled(false);
					locationToSpinner.setEnabled(false);
				}
				etcurrentreport.setText(currentReportName.replaceAll(
						"[^a-zA-Z.,]+", ""));
			}
		});

		builder.setCancelable(false);
		myDialog = builder.create();
		// Set ClickListener on btnSelectDate
		btndmreportfromdate.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// Show the DatePickerDialog
				DateTag = 30;
				showDialog(DATE_DIALOG_ID_FROM);
			}
		});

		// Set ClickListener on btnSelectDate
		btndmreporttodate.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// Show the DatePickerDialog
				DateTag = 40;
				showDialog(DATE_DIALOG_ID_TO);
			}
		});

		Button ExitFromReports = (Button) findViewById(R.id.exitfrom_reports);
		ExitFromReports.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				Intent myIntent = new Intent(view.getContext(),
						DM_InvoiceMenu.class);
				myIntent.putExtra("TAGID", "SALES");
				myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
						| Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivityForResult(myIntent, 0);
				finish();
			}

		});
		Button NewReport = (Button) findViewById(R.id.new_report);
		NewReport.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				etcurrentreport.setText("");
				btndmreportfromdate.setText("");
				btndmreporttodate.setText("");
			}

		});
		Button GenerateReports = (Button) findViewById(R.id.generate_report);
		GenerateReports.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				if (currentReportID.equals("1")) {
					/*
					 * Intent myIntent = new Intent(view.getContext(),
					 * DM_Retailer_Master_Report.class);
					 */
					Intent myIntent = new Intent(view.getContext(),
							DM_Target_VS_Actual_Report.class);
					myIntent.putExtra("REPORTNAME", currentReportName);
					myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
							| Intent.FLAG_ACTIVITY_NEW_TASK);
					startActivityForResult(myIntent, 0);
					finish();
				}

				else if (currentReportID.equals("16")) {
					/*
					 * Intent myIntent = new Intent(view.getContext(),
					 * DM_Retailer_Master_Report.class);
					 */
					Intent myIntent = new Intent(view.getContext(),
							DM_Target_VS_Actual_Report_Sales_Order.class);
					myIntent.putExtra("REPORTNAME", currentReportName);
					myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
							| Intent.FLAG_ACTIVITY_NEW_TASK);
					startActivityForResult(myIntent, 0);
					finish();
				}

				else if (currentReportID.equals("12")) {
					Intent myIntent = new Intent(view.getContext(),
							DM_Stock_InHand_Report.class);
					myIntent.putExtra("REPORTNAME", currentReportName);
					myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
							| Intent.FLAG_ACTIVITY_NEW_TASK);
					startActivityForResult(myIntent, 0);
					finish();
				}
				else {
					if (etcurrentreport.length() == 0) {
						final AlertDialog alertDialog = new AlertDialog.Builder(
								DM_Reports_Generator.this).create();
						// Setting Dialog Title
						alertDialog.setTitle("SELECT REPORT");
						// Setting Dialog Message
						alertDialog
								.setMessage("Select Report From The List...");
						// Setting Icon to Dialog
						alertDialog.setIcon(R.drawable.warning);
						// Setting OK Button
						alertDialog.setButton("OK",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int which) {
										alertDialog.dismiss();
									}
								});
						// Showing Alert Message
						alertDialog.show();
						alertDialog.setCanceledOnTouchOutside(false);
						return;
						
					}
					else if (currentReportID.equals("19")) {
						Intent myIntent = new Intent(view.getContext(),
								DM_Daily_Unloading_Report.class);
						myIntent.putExtra("REPORTNAME", currentReportName);
						myIntent.putExtra("FROMDATE", btndmreportfromdate
								.getText().toString());
						myIntent.putExtra("TODATE", btndmreporttodate
								.getText().toString());
						myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
								| Intent.FLAG_ACTIVITY_NEW_TASK);
						startActivityForResult(myIntent, 0);
						finish();
					}
					else if (btndmreportfromdate.length() == 0
							|| btndmreporttodate.length() == 0) {
						final AlertDialog alertDialog = new AlertDialog.Builder(
								DM_Reports_Generator.this).create();
						// Setting Dialog Title
						alertDialog.setTitle("SELECT DATE RANGE");
						// Setting Dialog Message
						alertDialog
								.setMessage("Select Report Generating Date Range...");
						// Setting Icon to Dialog
						alertDialog.setIcon(R.drawable.warning);
						// Setting OK Button
						alertDialog.setButton("OK",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int which) {
										alertDialog.dismiss();
									}
								});
						// Showing Alert Message
						alertDialog.show();
						alertDialog.setCanceledOnTouchOutside(false);
						return;
					} else {
						if (currentReportID.equals("2")) {
							Intent myIntent = new Intent(view.getContext(),
									DM_Sales_Summary_Report.class);
							myIntent.putExtra("REPORTNAME", currentReportName);
							myIntent.putExtra("FROMDATE", btndmreportfromdate
									.getText().toString());
							myIntent.putExtra("TODATE", btndmreporttodate
									.getText().toString());
							myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
									| Intent.FLAG_ACTIVITY_NEW_TASK);
							startActivityForResult(myIntent, 0);
							finish();
						} 
						else if (currentReportID.equals("17")) {
							Intent myIntent = new Intent(view.getContext(),
									DM_Sales_Summary_Report_SI.class);
							myIntent.putExtra("REPORTNAME", currentReportName);
							myIntent.putExtra("FROMDATE", btndmreportfromdate
									.getText().toString());
							myIntent.putExtra("TODATE", btndmreporttodate
									.getText().toString());
							myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
									| Intent.FLAG_ACTIVITY_NEW_TASK);
							startActivityForResult(myIntent, 0);
							finish();
						}
						else if (currentReportID.equals("18")) {
							Intent myIntent = new Intent(view.getContext(),
									DM_Sales_Summary_Report_SO.class);
							myIntent.putExtra("REPORTNAME", currentReportName);
							myIntent.putExtra("FROMDATE", btndmreportfromdate
									.getText().toString());
							myIntent.putExtra("TODATE", btndmreporttodate
									.getText().toString());
							myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
									| Intent.FLAG_ACTIVITY_NEW_TASK);
							startActivityForResult(myIntent, 0);
							finish();
						}
						
						else if (currentReportID.equals("3")) {
							Intent myIntent = new Intent(view.getContext(),
									DM_Sales_Summary_Report_Retailer.class);
							myIntent.putExtra("REPORTNAME", currentReportName);
							myIntent.putExtra("FROMDATE", btndmreportfromdate
									.getText().toString());
							myIntent.putExtra("TODATE", btndmreporttodate
									.getText().toString());
							myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
									| Intent.FLAG_ACTIVITY_NEW_TASK);
							startActivityForResult(myIntent, 0);
							finish();
						} else if (currentReportID.equals("4")) {
							Intent myIntent = new Intent(view.getContext(),
									DM_Sales_Summary_Report_Route.class);
							myIntent.putExtra("REPORTNAME", currentReportName);
							myIntent.putExtra("FROMDATE", btndmreportfromdate
									.getText().toString());
							myIntent.putExtra("TODATE", btndmreporttodate
									.getText().toString());
							myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
									| Intent.FLAG_ACTIVITY_NEW_TASK);
							startActivityForResult(myIntent, 0);
							finish();
						} else if (currentReportID.equals("5")) {
							Intent myIntent = new Intent(view.getContext(),
									DM_Sales_Summary_Report_Invoice.class);
							myIntent.putExtra("REPORTNAME", currentReportName);
							myIntent.putExtra("FROMDATE", btndmreportfromdate
									.getText().toString());
							myIntent.putExtra("TODATE", btndmreporttodate
									.getText().toString());
							myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
									| Intent.FLAG_ACTIVITY_NEW_TASK);
							startActivityForResult(myIntent, 0);
							finish();
						} else if (currentReportID.equals("6")) {
							FromtoLocationChk();
							if (CHKLOCTAG == 99) {
								Intent myIntent = new Intent(view.getContext(),
										DM_Loading_Report.class);
								myIntent.putExtra("REPORTNAME",
										currentReportName);
								myIntent.putExtra("FROMDATE",
										btndmreportfromdate.getText()
												.toString());
								// fromLocName, fromLocCode, toLocName,
								// toLocCode;
								myIntent.putExtra("LOCFROM", fromLocCode + "/"
										+ fromLocName);
								myIntent.putExtra("LOCTO", toLocCode + "/"
										+ toLocName);
								Log.v(TAG, "LOCFROM:" + fromLocCode + "TO : "
										+ toLocCode);
								myIntent.putExtra("TODATE", btndmreporttodate
										.getText().toString());
								myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
										| Intent.FLAG_ACTIVITY_NEW_TASK);
								startActivityForResult(myIntent, 0);
								finish();
							}

						} else if (currentReportID.equals("7")) {
							FromtoLocationChk();
							if (CHKLOCTAG == 99) {
								Intent myIntent = new Intent(view.getContext(),
										DM_Unloading_Report.class);
								myIntent.putExtra("REPORTNAME",
										currentReportName);
								myIntent.putExtra("FROMDATE",
										btndmreportfromdate.getText()
												.toString());
								// fromLocName, fromLocCode, toLocName,
								// toLocCode;
								myIntent.putExtra("LOCFROM", fromLocCode + "/"
										+ fromLocName);
								myIntent.putExtra("LOCTO", toLocCode + "/"
										+ toLocName);
								Log.v(TAG, "LOCFROM:" + fromLocCode + "TO : "
										+ toLocCode);
								myIntent.putExtra("TODATE", btndmreporttodate
										.getText().toString());
								myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
										| Intent.FLAG_ACTIVITY_NEW_TASK);
								startActivityForResult(myIntent, 0);
								finish();
							}

						} else if (currentReportID.equals("8")) {
							FromtoLocationChk();
							if (CHKLOCTAG == 99) {
								Intent myIntent = new Intent(view.getContext(),
										DM_DamageTransfer_Report.class);
								myIntent.putExtra("REPORTNAME",
										currentReportName);
								myIntent.putExtra("FROMDATE",
										btndmreportfromdate.getText()
												.toString());
								// fromLocName, fromLocCode, toLocName,
								// toLocCode;
								myIntent.putExtra("LOCFROM", fromLocCode + "/"
										+ fromLocName);
								myIntent.putExtra("LOCTO", toLocCode + "/"
										+ toLocName);
								Log.v(TAG, "LOCFROM:" + fromLocCode + "TO : "
										+ toLocCode);
								myIntent.putExtra("TODATE", btndmreporttodate
										.getText().toString());
								myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
										| Intent.FLAG_ACTIVITY_NEW_TASK);
								startActivityForResult(myIntent, 0);
								finish();
							}

						} else if (currentReportID.equals("9")) {
							Intent myIntent = new Intent(view.getContext(),
									DM_RetailerCard_Report.class);
							myIntent.putExtra("REPORTNAME", currentReportName);
							myIntent.putExtra("FROMDATE", btndmreportfromdate
									.getText().toString());
							myIntent.putExtra("TODATE", btndmreporttodate
									.getText().toString());
							myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
									| Intent.FLAG_ACTIVITY_NEW_TASK);
							startActivityForResult(myIntent, 0);
							finish();
						} else if (currentReportID.equals("10")) {
							Intent myIntent = new Intent(view.getContext(),
									DM_Retailer_Discount_Report.class);
							myIntent.putExtra("REPORTNAME", currentReportName);
							myIntent.putExtra("FROMDATE", btndmreportfromdate
									.getText().toString());
							myIntent.putExtra("TODATE", btndmreporttodate
									.getText().toString());
							myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
									| Intent.FLAG_ACTIVITY_NEW_TASK);
							startActivityForResult(myIntent, 0);
							finish();
						} else if (currentReportID.equals("11")) {
							Intent myIntent = new Intent(view.getContext(),
									DM_Van_Sales_Summary_Report.class);
							myIntent.putExtra("REPORTNAME", currentReportName);
							myIntent.putExtra("FROMDATE", btndmreportfromdate
									.getText().toString());
							myIntent.putExtra("TODATE", btndmreporttodate
									.getText().toString());
							myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
									| Intent.FLAG_ACTIVITY_NEW_TASK);
							startActivityForResult(myIntent, 0);
							finish();
						} else if (currentReportID.equals("13")) {
							Intent myIntent = new Intent(view.getContext(),
									DM_CollectionSumReceipt_Report.class);
							myIntent.putExtra("REPORTNAME", currentReportName);
							myIntent.putExtra("FROMDATE", btndmreportfromdate
									.getText().toString());
							myIntent.putExtra("TODATE", btndmreporttodate
									.getText().toString());
							myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
									| Intent.FLAG_ACTIVITY_NEW_TASK);
							startActivityForResult(myIntent, 0);
							finish();
						} else if (currentReportID.equals("14")) {
							Intent myIntent = new Intent(view.getContext(),
									DM_CollectionSumRetailer_Report.class);
							myIntent.putExtra("REPORTNAME", currentReportName);
							myIntent.putExtra("FROMDATE", btndmreportfromdate
									.getText().toString());
							myIntent.putExtra("TODATE", btndmreporttodate
									.getText().toString());
							myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
									| Intent.FLAG_ACTIVITY_NEW_TASK);
							startActivityForResult(myIntent, 0);
							finish();
						}

						else if (currentReportID.equals("15")) {
							Intent myIntent = new Intent(view.getContext(),
									DM_Sales_Summary_Report_SalOrder.class);
							myIntent.putExtra("REPORTNAME", currentReportName);
							myIntent.putExtra("FROMDATE", btndmreportfromdate
									.getText().toString());
							myIntent.putExtra("TODATE", btndmreporttodate
									.getText().toString());
							myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
									| Intent.FLAG_ACTIVITY_NEW_TASK);
							startActivityForResult(myIntent, 0);
							finish();
						}
						

					}
				}

			}

		});
	}

	protected void resetFilters() {
		// TODO Auto-generated method stub
		btndmreportfromdate.setText("");
		btndmreporttodate.setText("");
	}

	protected void FromtoLocationChk() {
		// TODO Auto-generated method stub
		if (fromLocCode.equals("0") || toLocCode.equals("0")) {
			final AlertDialog alertDialog = new AlertDialog.Builder(
					DM_Reports_Generator.this).create();
			// Setting Dialog Title
			alertDialog.setTitle("SELECT LOCATIONS");
			// Setting Dialog Message
			alertDialog
					.setMessage("Select Stock Loading From - To Locations...");
			// Setting Icon to Dialog
			alertDialog.setIcon(R.drawable.warning);
			// Setting OK Button
			alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					alertDialog.dismiss();
				}
			});
			// Showing Alert Message
			alertDialog.show();
			alertDialog.setCanceledOnTouchOutside(false);
			return;
		} else {
			CHKLOCTAG = 99;
		}

	}

	private void loadToMainStoreLocationSpinner() {
		// TODO Auto-generated method stub
		m_adapterForSpinnerToLoc = new ArrayAdapter<CharSequence>(this,
				android.R.layout.simple_spinner_item);
		m_adapterForSpinnerToLoc
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor curToLoc = null;
		try {
			curToLoc = mydb.query(ITEM_LOCATION, null, "LoctCode ='LT1'", null,
					null, null, null, null);
		} catch (Exception e) {
		}
		if (curToLoc != null) {
			if (curToLoc.getCount() > 0) {
				m_adapterForSpinnerToLoc.add("SELECT TO LOCATION");
				while (curToLoc.moveToNext()) {
					m_adapterForSpinnerToLoc.add(curToLoc.getString(10));
				}
			}
			curToLoc.close();
		}
		mydb.close();

	}

	private void loadFromRepLocationSpinner() {

		// TODO Auto-generated method stub
		m_adapterForSpinner = new ArrayAdapter<CharSequence>(this,
				android.R.layout.simple_spinner_item);
		m_adapterForSpinner
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor curFromLoc = null;
		try {
			curFromLoc = mydb.query(ITEM_LOCATION, null, "LoctCode ='LT3'",
					null, null, null, null, null);
		} catch (Exception e) {
		}
		if (curFromLoc != null) {
			if (curFromLoc.getCount() > 0) {
				m_adapterForSpinner.add("SELECT FROM LOCATION");
				while (curFromLoc.moveToNext()) {
					m_adapterForSpinner.add(curFromLoc.getString(10));
				}
			}
			curFromLoc.close();
		}
		mydb.close();
	}

	private void loadToRepLocationSpinner() {
		// TODO Auto-generated method stub
		m_adapterForSpinnerToLoc = new ArrayAdapter<CharSequence>(this,
				android.R.layout.simple_spinner_item);
		m_adapterForSpinnerToLoc
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor curToLoc = null;
		try {
			curToLoc = mydb.query(ITEM_LOCATION, null, "LoctCode ='LT3'", null,
					null, null, null, null);
		} catch (Exception e) {

		}
		if (curToLoc != null) {

			if (curToLoc.getCount() > 0) {
				m_adapterForSpinnerToLoc.add("SELECT TO LOCATION");
				while (curToLoc.moveToNext()) {
					m_adapterForSpinnerToLoc.add(curToLoc.getString(10));
				}
			}
			curToLoc.close();
		}
		mydb.close();
	}

	private void loadFromMainStoreLocationSpinner() {
		// TODO Auto-generated method stub
		m_adapterForSpinner = new ArrayAdapter<CharSequence>(this,
				android.R.layout.simple_spinner_item);
		m_adapterForSpinner
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor curFromLoc = null;
		try {
			curFromLoc = mydb.query(ITEM_LOCATION, null, "LoctCode ='LT1'",
					null, null, null, null, null);
		} catch (Exception e) {
		}
		if (curFromLoc != null) {
			if (curFromLoc.getCount() > 0) {
				m_adapterForSpinner.add("SELECT FROM LOCATION");
				while (curFromLoc.moveToNext()) {
					m_adapterForSpinner.add(curFromLoc.getString(10));
				}
			}
			curFromLoc.close();
		}
		mydb.close();
	}

	protected void reloadView() {
		// TODO Auto-generated method stub
		String currentReport = currentReportID + "/" + currentReportName;
		Intent myIntent = new Intent(this, DM_Reports_Generator.class);
		myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
				| Intent.FLAG_ACTIVITY_NEW_TASK);
		myIntent.putExtra("TAGID", currentReport);
		startActivityForResult(myIntent, 0);
	}

	private void loadFromLocationSpinner() {
		// TODO Auto-generated method stub
		m_adapterForSpinner = new ArrayAdapter<CharSequence>(this,
				android.R.layout.simple_spinner_item);
		m_adapterForSpinner
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor curFromLoc = null;
		try {
			curFromLoc = mydb.query(ITEM_LOCATION, null, "LoctCode !='LT2'",
					null, null, null, null, null);
		} catch (Exception e) {

		}
		if (curFromLoc != null) {

			if (curFromLoc.getCount() > 0) {
				m_adapterForSpinner.add("SELECT FROM LOCATION");
				while (curFromLoc.moveToNext()) {
					m_adapterForSpinner.add(curFromLoc.getString(10));

				}
			}
			curFromLoc.close();

		}

		mydb.close();

	}

	private void loadToDamageLocationSpinner() {
		// TODO Auto-generated method stub
		m_adapterForSpinnerToLoc = new ArrayAdapter<CharSequence>(this,
				android.R.layout.simple_spinner_item);
		m_adapterForSpinnerToLoc
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor curToLoc = null;
		try {
			curToLoc = mydb.query(ITEM_LOCATION, null, "LoctCode ='LT2'", null,
					null, null, null, null);
		} catch (Exception e) {

		}
		if (curToLoc != null) {

			if (curToLoc.getCount() > 0) {
				m_adapterForSpinnerToLoc.add("SELECT TO LOCATION");
				while (curToLoc.moveToNext()) {
					m_adapterForSpinnerToLoc.add(curToLoc.getString(10));

				}
			}
			curToLoc.close();

		}

		mydb.close();

	}

	@Override
	protected Dialog onCreateDialog(int id) {

		switch (id) {
		case DATE_DIALOG_ID_FROM:
			// create a new DatePickerDialog with values you want to show
			return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
					mDay);
		case DATE_DIALOG_ID_TO:
			// create a new DatePickerDialog with values you want to show
			return new DatePickerDialog(this, mDateSetListenerTo, mYear,
					mMonth, mDay);
		}
		return InvoiceHeaderAlert;
	}

	// Register DatePickerDialog listener
	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
		// the callback received when the user "sets" the Date in the
		// DatePickerDialog
		public void onDateSet(DatePicker view, int yearSelected,
				int monthOfYear, int dayOfMonth) {
			mYear = yearSelected;
			mMonth = monthOfYear + 1;
			mDay = dayOfMonth;
			String mMonthStr = Integer.toString(mMonth);
			String mDayStr = Integer.toString(mDay);
			if (mMonthStr.length() == 1) {
				mMonthStr = "0" + mMonthStr;
			}
			if (mDayStr.length() == 1) {
				mDayStr = "0" + mDayStr;
			}
			btndmreportfromdate
					.setText(mDayStr + "/" + mMonthStr + "/" + mYear);
			mMonth = mMonth - 1;
		}
	};
	// Register DatePickerDialog listener
	private DatePickerDialog.OnDateSetListener mDateSetListenerTo = new DatePickerDialog.OnDateSetListener() {
		// the callback received when the user "sets" the Date in the
		// DatePickerDialog
		public void onDateSet(DatePicker view, int yearSelected,
				int monthOfYear, int dayOfMonth) {
			mYear = yearSelected;
			mMonth = monthOfYear + 1;
			mDay = dayOfMonth;
			String mMonthStr = Integer.toString(mMonth);
			String mDayStr = Integer.toString(mDay);
			if (mMonthStr.length() == 1) {
				mMonthStr = "0" + mMonthStr;
			}
			if (mDayStr.length() == 1) {
				mDayStr = "0" + mDayStr;
			}
			btndmreporttodate.setText(mDayStr + "/" + mMonthStr + "/" + mYear);
			mMonth = mMonth - 1;
		}
	};
}
